package com.learn.shop.controller;

import com.learn.shop.constant.R;
import com.learn.shop.pojo.entriy.CrawlingNovelData;
import com.learn.shop.pojo.vo.BatchCrawlingNovelVo;
import com.learn.shop.pojo.vo.CrawlingNovelVo;
import com.learn.shop.service.CrawlingNovelDataService;
import com.learn.shop.service.CrawlingNovelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("xbiquge")
@Slf4j
public class XbiqugeCrawlingController {

    private static final Logger logger = LoggerFactory.getLogger(XbiqugeCrawlingController.class);

    @Autowired
    @Qualifier("CrawlingNovelImplV3")
    private CrawlingNovelService crawlingNovelService;

    @Autowired
    private CrawlingNovelDataService dataService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("save")
    public R crawlingSave() throws IOException {

        Document document = Jsoup.connect("https://www.xbiquge.la/xiaoshuodaquan/")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cache-Control", "no-cache")
                .header("Connection", "keep-alive")
                .header("Pragma", "no-cache")
                .header("Upgrade-Insecure-Requests", "1")
                .timeout(8000)
                .userAgent("Mozilla")//模拟浏览器
                .get();

        //删除所有记录
        try {
            dataService.deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("清空小说首页记录表失败!");
        }
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        try {
            Element main = document.getElementById("main");
            assert main != null;
            Elements list = main.select("li>a");
            System.out.println(list.size());
            for (Element element : list) {

                Runnable task = () -> {
                    log.info("使用线程池分批次解析HTML ： " + Thread.currentThread().getName());
                    String href = element.attr("href");
                    String text = element.text();
                    logger.info(href + "---->" + text);
                    CrawlingNovelData novelData = new CrawlingNovelData();
                    novelData.setNovelName(text);
                    novelData.setUrl(href);
                    dataService.insert(novelData);
                };
                executorService.submit(task);
            }
            executorService.shutdown();

            logger.info("保存小说首页信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("保存完成!");
    }


    @GetMapping("/batchCrawling/{count}")
    @ApiOperation("批量爬取小说")
    public R batchCrawlingNovel(@PathVariable int count) {
        Set<String> collectionNames = mongoTemplate.getCollectionNames();
        BatchCrawlingNovelVo novelVo = new BatchCrawlingNovelVo();
        novelVo.setNovelName(collectionNames);
        novelVo.setCount(count);

        List<CrawlingNovelData> list = dataService.list(novelVo);

        for (CrawlingNovelData crawlingNovelData : list) {
            CrawlingNovelVo crawlingNovelVo = new CrawlingNovelVo();
            crawlingNovelVo.setNovelName(crawlingNovelData.getNovelName());
            crawlingNovelVo.setUrl(crawlingNovelData.getUrl());
            crawlingNovelVo.setUrlSplit("la");
            try {
                crawlingNovelService.novel(crawlingNovelVo);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("批量爬取-->解析HTML出错");
            }
        }
        return R.ok("解析完毕!");

    }
}
